package code;

import java.io.File;

import javax.swing.filechooser.FileSystemView;

import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.util.Callback;

public class FolderPane extends BorderPane {
	private HBox titleBox;
	private Label titleLabel;
	private TreeView<File> folderTree;

	public TreeView<File> getFolderTree() {
		return folderTree;
	}

	public FolderPane() {
		init();
	}

	private void init() {
		titleLabel = new Label("My Computer");
		titleLabel.setFont(new Font("Microsoft YaHei", 15.0));
		titleLabel.setTextFill(Color.BLACK);

		titleBox = new HBox();
		titleBox.setPadding(new Insets(15));
		titleBox.getChildren().add(titleLabel);

		FolderTreeItem root = new FolderTreeItem(new File("ROOT"), true);
		root.loadChildren();

		folderTree = new TreeView<>(root);
		folderTree.setShowRoot(false);
		folderTree.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);

		folderTree.setCellFactory(new Callback<TreeView<File>, TreeCell<File>>() {
			@Override
			public TreeCell<File> call(TreeView<File> treeView) {
				return new TreeCell<File>() {

					@Override
					protected void updateItem(File item, boolean empty) {
						super.updateItem(item, empty);

						if (empty) {
							setText(null);
						} else {
							FileSystemView fileSystemView = FileSystemView.getFileSystemView();
							String text = fileSystemView.getSystemDisplayName(item);
							setText(text);
						}
					}

				};
			}
		});

		for (TreeItem<File> item : root.getChildren()) {
			((FolderTreeItem) item).loadChildren();
		}

		this.setTop(titleBox);
		this.setCenter(folderTree);
	}

}
